//Confirm使用方示例

// showConfirm('你好，恭喜通过测试！',
//   function() {
//     alert('确认回调事件');
//   },
//   function() {
//     alert('关闭');
//   },
//   '前去绑定',
//   '取消');

//Alert使用方法示例

// showAlert("low不low！", function() {
//     alert('点击了确认按钮');
//   },
//   '确认按钮操作');

/**
 * 分隔获取各个参数
 * 根据URL地址获取其参数
 */
function UrlSearch() {
  var name, value;
  var str = location.href; //取得整个地址栏
  var num = str.indexOf("?");
  str = str.substr(num + 1);
  var arr = str.split("&"); //各个参数放到数组里
  for (var i = 0; i < arr.length; i++) {
    num = arr[i].indexOf("=");
    if (num > 0) {
      name = arr[i].substring(0, num);
      value = arr[i].substr(num + 1);
      this[name] = value;
    }
  }
}

/**
 * 调用微信端确认Confrim模态框
 * 
 * @param {any} wordStr  提示语
 * @param {any} confirmCallback  确认回调事件
 * @param {any} cancelCallback 取消回调事件
 * @param {any} confrimStr 确认按钮文字
 * @param {any} cancelStr 取消按钮文字
 */
function showConfirm(wordStr, confirmCallback, cancelCallback, confrimStr, cancelStr) {
  wordStr = wordStr || "提示！";
  cancelStr = cancelStr || "关闭";
  confrimStr = confrimStr || "确认";
  confirmCallback = confirmCallback ? confirmCallback : function() {};
  cancelCallback = cancelCallback ? cancelCallback : function() {};
  var str = '<div id="ConfirmDialogs">' +
    '          <div class="js_dialog" id="iosDialog2" style="display: block;">' +
    '            <div class="weui-mask"></div>' +
    '            <div class="weui-dialog">' +
    '              <div class="weui-dialog__bd" style="font-size: 18px;color: #333;">' + wordStr + '</div>' +
    '              <div class="weui-dialog__ft" style="padding: 10px;">' +
    '                <a class="btn btn-skip cancel-block" style="width: 40%;margin:0;">' + cancelStr + '</a>' +
    '                <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary btn btn-yes" style="padding:5px;line-height: 150%;">' + confrimStr + '</a>' +
    '              </div>' +
    '            </div>' +
    '          </div>' +
    '        </div>';

  $(".page").append(str);
  //关闭事件
  $('#ConfirmDialogs').on('click', '.cancel-block', function() {
    //关闭回调事件
    cancelCallback();
    //关闭模态框 
    $('#ConfirmDialogs').fadeOut(200).remove();

  });


  //确认事件
  $('#ConfirmDialogs').on('click', '.btn-yes', function() {
    //确定回调事件
    confirmCallback();
    //关闭模态框
    $('#ConfirmDialogs').fadeOut(200).remove();
  });
}

/**
 * 调用微信端确认Confrim模态框(带图)
 * 
 * @param {any} wordStr  提示语
 * @param {any} confirmCallback  确认回调事件
 * @param {any} cancelCallback 取消回调事件
 * @param {any} confrimStr 确认按钮文字
 * @param {any} cancelStr 取消按钮文字
 */
function showConfirmWithPic(wordStr, confirmCallback, cancelCallback, confrimStr, cancelStr, describe, picsrc) {
  wordStr = wordStr || "提示！";
  cancelStr = cancelStr || "关闭";
  confrimStr = confrimStr || "确认";
  describe = describe || "";
  confirmCallback = confirmCallback ? confirmCallback : function() {};
  cancelCallback = cancelCallback ? cancelCallback : function() {};
  var str = '<div id="ConfirmDialogs">' +
    '          <div class="js_dialog" id="iosDialog2" style="display: block;">' +
    '            <div class="weui-mask"></div>' +
    '            <div class="weui-dialog">' +
    '              <div class="weui-dialog__bd" style="font-size: 16px;color: #333;">' +
    '				<img class="" src="' + picsrc + '" style="width: 100%;margin-bottom:10px;"/>' +
    wordStr +
    '                <br/><span class="weui-dialog__bd_sub" style="font-size: 13px;color: #999;margin-top: 15px;">' + describe + '</span>' +
    '				</div>' +
    '              <div class="weui-dialog__ft" style="padding: 10px;">' +
    '                <a class="btn btn-skip cancel-block" style="width: 40%;margin:0;">' + cancelStr + '</a>' +
    '                <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary btn btn-yes" style="padding:5px;line-height: 150%;">' + confrimStr + '</a>' +
    '              </div>' +
    '            </div>' +
    '          </div>' +
    '        </div>';

  $(".page").append(str);
  //关闭事件
  $('#ConfirmDialogs').on('click', '.cancel-block', function() {
    //关闭回调事件
    cancelCallback();
    //关闭模态框 
    $('#ConfirmDialogs').fadeOut(200).remove();

  });


  //确认事件
  $('#ConfirmDialogs').on('click', '.btn-yes', function() {
    //确定回调事件
    confirmCallback();
    //关闭模态框
    $('#ConfirmDialogs').fadeOut(200).remove();
  });
}


/**
 * 调用微信端确认Alert模态框
 * 
 * @param {any} wordStr  提示语
 * @param {any} confirmCallback  确认回调事件
 * @param {any} confrimStr 确认按钮文字
 */
function showAlert(wordStr, confirmCallback, confrimStr) {
  wordStr = wordStr || "提示！";
  confrimStr = confrimStr || "确定";
  confirmCallback = confirmCallback ? confirmCallback : function() {};
  var str = ' <div id="AlertDialogs">' +
    '          <div class="js_dialog" id="iosDialog2" style="display: block;">' +
    '            <div class="weui-mask"></div>' +
    '            <div class="weui-dialog">' +
    '              <div class="weui-dialog__bd" style="font-size: 18px;color: #333;">' + wordStr +
    '				</div>' +
    '              <div class="weui-dialog__ft" style="padding: 10px;">' +
    '                <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary btn big-confrim-btn" style="padding:5px;line-height: 150%;">' + confrimStr + '</a>' +
    '              </div>' +
    '            </div>' +
    '          </div>' +
    '        </div>';
  $(".page").append(str);
  //确认事件
  $('#AlertDialogs').on('click', '.big-confrim-btn', function() {
    //确定回调事件
    confirmCallback();
    //关闭模态框
    $('#AlertDialogs').fadeOut(200).remove();
  });

}

/**
 * 调用微信端确认Alert模态框(带描述语)
 * 
 * @param {any} wordStr  提示语
 * @param {any} confirmCallback  确认回调事件
 * @param {any} confrimStr 确认按钮文字
 */
function showAlertWithDescribe(wordStr, confirmCallback, confrimStr, describe) {
  wordStr = wordStr || "提示！";
  confrimStr = confrimStr || "确定";
  describe = describe || "";
  confirmCallback = confirmCallback ? confirmCallback : function() {};
  var str = ' <div id="AlertDialogs">' +
    '          <div class="js_dialog" id="iosDialog2" style="display: block;">' +
    '            <div class="weui-mask"></div>' +
    '            <div class="weui-dialog">' +
    '              <div class="weui-dialog__bd" style="font-size: 18px;color: #333;">' + wordStr +
    '                <br/><span class="weui-dialog__bd_sub" style="font-size: 13px;color: #999;margin-top: 15px;">' + describe + '</span>' +
    '				</div>' +
    '              <div class="weui-dialog__ft" style="padding: 10px;">' +
    '                <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary btn big-confrim-btn" style="padding:5px;line-height: 150%;">' + confrimStr + '</a>' +
    '              </div>' +
    '            </div>' +
    '          </div>' +
    '        </div>';
  $(".page").append(str);
  //确认事件
  $('#AlertDialogs').on('click', '.big-confrim-btn', function() {
    //确定回调事件
    confirmCallback();
    //关闭模态框
    $('#AlertDialogs').fadeOut(200).remove();
  });

}

/**
 * 调用微信端确认Alert模态框(带图)
 * 
 * @param {any} wordStr  提示语
 * @param {any} confirmCallback  确认回调事件
 * @param {any} confrimStr 确认按钮文字
 */
function showAlertWithPic(wordStr, confirmCallback, confrimStr, picsrc) {
  wordStr = wordStr || "提示！";
  confrimStr = confrimStr || "确定";
  confirmCallback = confirmCallback ? confirmCallback : function() {};
  var str = ' <div id="AlertDialogsPic">' +
    '          <div class="js_dialog" id="iosDialog2Pic" style="display: block;margin-top: 120px;">' +
    '            <div class="weui-mask"></div>' +
    '            <div class="weui-dialog">' +
    '              <div class="weui-dialog__bd" style="font-size: 16px;color: #333;">' +
    '              <img class="" src="' + picsrc + '" style="width: 100%;margin-bottom:10px;"/>' +
    wordStr + '</div>' +
    '              <div class="weui-dialog__ft" style="padding: 10px;">' +
    '                <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary btn big-confrim-btn" style="padding:5px;line-height: 150%;">' +
    '              <img class="" src="../../static/images/weixin/company/next.png" style="width: 18px;height:18px;margin-bottom:-2px;margin-right:5px;"/>' +
    confrimStr + '</a>' +
    '              </div>' +
    '            </div>' +
    '          </div>' +
    '        </div>';
  $(".page").append(str);
  //确认事件
  $('#AlertDialogsPic').on('click', '.big-confrim-btn', function() {
    //确定回调事件
    confirmCallback();
    //关闭模态框
    $('#AlertDialogsPic').fadeOut(200).remove();
  });

}


function getUrlArea() {
  var r = window.location.host;
  return r;
}


//获取当前调用协议
function getProtocol() {
  var p = window.location.protocol + "//";
  return p;
}

/**
 * 电话校验
 * 
 * @param {any} phone 手机号
 * @returns 是否有效 T/F
 */
function checkPhone(phone) {
  if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
    return false;
  } else {
    return true;
  }
}

/**
 * 邮箱校验方法
 * 
 * @param {any} mail 邮箱地址
 * @returns 是否有效 T/F
 */
function checkMail(mail) {
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (filter.test(mail)) {
    return true;
  } else {
    return false;
  }
}

//分页控件的初始化
function initPageDiv($dom, now, all, each, $dom2, change) {
  if (parseInt(now) > parseInt(all)) {
    return;
  }
  var options = {
    bootstrapMajorVersion: 3,
    currentPage: now, // 当前页
    totalPages: all, // 共几页
    numberOfPages: each, // 每次显示几页
    itemTexts: function(type, page, current) { // 修改显示文字
      switch (type) {
        case "first":
          return "首页";
        case "prev":
          return "<";
        case "next":
          return ">";
        case "last":
          return "尾页";
        case "page":
          return page;
      }
    },
    onPageClicked: function(event, originalEvent, type, page) { // 异步换页
      $dom2.val(page);
      change();
    }
  }
  $dom.bootstrapPaginator(options);
};